var router = require('koa-router')({
    prefix: '/project/:id'
});
var models = require('./../../../models');
var q = require('q');

/** 项目的全站仪配置 */
router.get('/stationconfig', function* (next) {
    var stations = yield this.state.project.getStationConfigs();
    this.render('project/station.config.jade', {
        project: this.state.project,
        stations: stations,
        tpsCom: this.session.tpsCom || 'COM1'
    });
});

/** 项目的全站仪配置,增加全站仪坐标 */
router.post('/stationconfig', function* (next) {
    var body = this.request.body;
    yield models.StationConfig.create(body);
    this.redirect(`/project/${this.params.id}/stationconfig`);
});

/** 获取某个全站仪 */
router.use('/stationconfig/:stationId', function* (next) {
    this.state.station = yield models.StationConfig.findById(parseInt(this.params.stationId));
    yield next;
});

/** 测试全站仪的连通性,顺便初始化tpsHelper */
router.get('/stationconfig/:stationId/test/:com', function* (next) {
    // this.cookies.set('tpsCom', this.params.com);
    this.session.tpsCom = this.params.com;
    yield this.state.pipeCtrl.initialTps(this.params.com, this.state.station);
    var coor = yield this.state.pipeCtrl.tpsHelper.geoCom.getStationCoor();
    yield this.state.pipeCtrl.tpsHelper.closePort();
    if (coor) {
        this.body = coor;
    } else {
        this.body = false;
    }
});

/** 删除全站仪坐标 */
router.get('/stationconfig/:stationId/delete', function* (next) {
    var station = this.state.station;
    yield station.destroy();
    this.redirect(`/project/${this.params.id}/stationconfig`);
});

module.exports = router.routes();